#include <SqList.h>

// Insert
bool ListInsert(SqList &L, int i, ElemType e) {

    if (i<1 || i>L.length) // invalide position
        return false;

    if (L.length >= Maxsize) // storage is full
        return false;

    for (int j=L.length; j>=i; j--) { // i ~ L.length move backward 1 (include i)
        L.data[j] = L.data[j-1];
    }

    L.data[j-1] = e; // insert 'e' to j-1

    L.length++; // length+1
    return true;
}

// Delete
ListDelete(SqList &L, int i, ElemType e) {
    // invalide position
    if (i<1 || i>L.length) 
        return false;

    // get value of deleted item
    e = L.data[i]; 
    
    // move postion (one by one)
    for (int j=i; j<=L.length; j++) 
        L.data[j] = L.data[j+1];

    // length-1
    L.length--; 
    return true;
}

// Locate by value
int LocateElem(SqList L, ElemType e) {
    int i;
    for (i=0; i<L.length; i++) { // Notice! i=0
        if (L.data[i] == e) {
            return i+1; 
        }
    }
    return 0;
}
